package cn.android.weblog;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import java.util.Map;

import cn.android.weblog.api.LoginAPI;
import cn.android.weblog.domain.User;
import cn.android.weblog.redisdata.LOGIN;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "login";
    private Button loginBtn;
    private EditText useridEt;
    private EditText passEt;

    //private TextView toRegist;
    private LoginAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        loginBtn = (Button) findViewById(R.id.login_btn);
    //    toRegist = (TextView)findViewById(R.id.toRegist);
        useridEt=(EditText)findViewById(R.id.login_id);
        passEt=(EditText)findViewById(R.id.login_pass);
        api=new LoginAPI();

        // 单击登录按钮
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickLogin();
            }
        });

        // 单击注册按钮
/*        toRegist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegistActivity.class);
                startActivity(intent);
            }
        });*/

    }


    /**
     * 登录 t
     */
    public void onClickLogin() {
        String userid = useridEt.getText().toString().trim();
        String pass = passEt.getText().toString().trim();
        if (userid.equals("")) {
            Toast.makeText(this, "账号不能为空！", Toast.LENGTH_LONG).show();
            return;
        }
        if (pass.equals("")) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_LONG).show();
            return;
        }

        String login = api.login(userid, pass);
        Map<String,Object> result=JSON.parseObject(login);
        if (result.get("Msg") == null) {
            Toast.makeText(this,"登录成功！",Toast.LENGTH_LONG).show();
            // 将当前登录对象信息存储到类中
            LOGIN.USER=JSON.parseObject(result.get("user").toString(), User.class);
            startActivity(new Intent(LoginActivity.this,MyActivity.class));
        }else{
            Toast.makeText(this,result.get("Msg").toString(),Toast.LENGTH_LONG).show();
        }

    }
}
